package com.zhika.zishi.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.zhika.zishi.R;
import com.zhika.zishi.activity.TaInfoActivity;
import com.zhika.zishi.beans.AnswerInfoBean;

import java.util.ArrayList;

/**
 * Created by shi-02 on 2015/4/9.
 * 回答内容的适配器
 */
public class AnswerInfoAdapter extends BaseAdapter {

    private Context context;

    private ArrayList<AnswerInfoBean> answerInfoList;

    public AnswerInfoAdapter(Context context,ArrayList<AnswerInfoBean> answerInfoList){
        this.context = context;

        this.answerInfoList = answerInfoList;
    }

    @Override
    public Object getItem(int position) {
        return answerInfoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public int getCount() {
        return answerInfoList.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        ViewHolder holder;
        if (convertView==null){
            view = LayoutInflater.from(context).inflate(R.layout.answer_item_layout,null);
            holder = new ViewHolder();
            holder.headerImageView = (ImageView) view.findViewById(R.id.answer_item_header_image_view);
            holder.nicknameText = (TextView) view.findViewById(R.id.answer_item_nickname_text);
            holder.likeNumText = (TextView) view.findViewById(R.id.answer_item_like_text);
            holder.pictureImageView = (ImageView) view.findViewById(R.id.answer_item_picture_image_view);
            holder.contentText = (TextView) view.findViewById(R.id.answer_item_content_text);
            view.setTag(holder);
        }else {
            view = convertView;
            holder  = (ViewHolder) view.getTag();
            resetViewHolder(holder);
        }

        AnswerInfoBean bean = (AnswerInfoBean) getItem(position);
        holder.headerImageView.setImageResource(R.drawable.ic_launcher);
        holder.nicknameText.setText(bean.getNicknameStr());
        holder.likeNumText.setText(bean.getLikeNumStr());
        holder.pictureImageView.setImageResource(R.drawable.test_1);
        holder.contentText.setText(bean.getAnswerContent());

        holder.headerImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, TaInfoActivity.class);
                context.startActivity(intent);
            }
        });
        return view;
}

    private void resetViewHolder(ViewHolder holder){
        holder.headerImageView.setImageResource(0);
        holder.nicknameText.setText(null);
        holder.likeNumText.setText(null);
        holder.pictureImageView.setImageResource(0);
        holder.contentText.setText(null);
    }

    private class ViewHolder{
        private ImageView headerImageView;

        private TextView nicknameText;

        private TextView likeNumText;

        private ImageView pictureImageView;

        private TextView contentText;
    }
}
